<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Exercise 4.3: Solve a simple QP with inequality constraints</title>
<link rel="canonical" href="/Users/mcgrant/Repos/CVX/examples/cvxbook/Ch04_cvx_opt_probs/html/ex_4_3.html">
<link rel="stylesheet" href="../../../examples.css" type="text/css">
</head>
<body>
<div id="header">
<h1>Exercise 4.3: Solve a simple QP with inequality constraints</h1>
Jump to:&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#source">Source code</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#output">Text output</a>
&nbsp;&nbsp;&nbsp;&nbsp;
Plots
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../../index.html">Library index</a>
</div>
<div id="content">
<a id="source"></a>
<pre class="codeinput">
<span class="comment">% From Boyd &amp; Vandenberghe, "Convex Optimization"</span>
<span class="comment">% Jo&euml;lle Skaf - 09/26/05</span>
<span class="comment">%</span>
<span class="comment">% Solves the following QP with inequality constraints:</span>
<span class="comment">%           minimize    1/2x'*P*x + q'*x + r</span>
<span class="comment">%               s.t.    -1 &lt;= x_i &lt;= 1      for i = 1,2,3</span>
<span class="comment">% Also shows that the given x_star is indeed optimal</span>

<span class="comment">% Generate data</span>
P = [13 12 -2; 12 17 6; -2 6 12];
q = [-22; -14.5; 13];
r = 1;
n = 3;
x_star = [1;1/2;-1];

<span class="comment">% Construct and solve the model</span>
fprintf(1,<span class="string">'Computing the optimal solution ...'</span>);
cvx_begin
    variable <span class="string">x(n)</span>
    minimize ( (1/2)*quad_form(x,P) + q'*x + r)
    x &gt;= -1;
    x &lt;=  1;
cvx_end
fprintf(1,<span class="string">'Done! \n'</span>);

<span class="comment">% Display results</span>
disp(<span class="string">'------------------------------------------------------------------------'</span>);
disp(<span class="string">'The computed optimal solution is: '</span>);
disp(x);
disp(<span class="string">'The given optimal solution is: '</span>);
disp(x_star);
</pre>
<a id="output"></a>
<pre class="codeoutput">
Computing the optimal solution ... 
Calling Mosek 9.1.9: 11 variables, 4 equality constraints
   For improved efficiency, Mosek is solving the dual problem.
------------------------------------------------------------

MOSEK Version 9.1.9 (Build date: 2019-11-21 11:32:15)
Copyright (c) MOSEK ApS, Denmark. WWW: mosek.com
Platform: MACOSX/64-X86

Problem
  Name                   :                 
  Objective sense        : min             
  Type                   : CONIC (conic optimization problem)
  Constraints            : 4               
  Cones                  : 1               
  Scalar variables       : 11              
  Matrix variables       : 0               
  Integer variables      : 0               

Optimizer started.
Presolve started.
Linear dependency checker started.
Linear dependency checker terminated.
Eliminator started.
Freed constraints in eliminator : 0
Eliminator terminated.
Eliminator started.
Freed constraints in eliminator : 0
Eliminator terminated.
Eliminator - tries                  : 2                 time                   : 0.00            
Lin. dep.  - tries                  : 1                 time                   : 0.00            
Lin. dep.  - number                 : 0               
Presolve terminated. Time: 0.00    
Problem
  Name                   :                 
  Objective sense        : min             
  Type                   : CONIC (conic optimization problem)
  Constraints            : 4               
  Cones                  : 1               
  Scalar variables       : 11              
  Matrix variables       : 0               
  Integer variables      : 0               

Optimizer  - threads                : 8               
Optimizer  - solved problem         : the primal      
Optimizer  - Constraints            : 4
Optimizer  - Cones                  : 1
Optimizer  - Scalar variables       : 11                conic                  : 5               
Optimizer  - Semi-definite variables: 0                 scalarized             : 0               
Factor     - setup time             : 0.00              dense det. time        : 0.00            
Factor     - ML order time          : 0.00              GP order time          : 0.00            
Factor     - nonzeros before factor : 10                after factor           : 10              
Factor     - dense dim.             : 0                 flops                  : 8.80e+01        
ITE PFEAS    DFEAS    GFEAS    PRSTATUS   POBJ              DOBJ              MU       TIME  
0   2.2e+01  2.0e+00  1.0e+00  0.00e+00   0.000000000e+00   0.000000000e+00   1.0e+00  0.00  
1   5.2e+00  4.7e-01  4.3e-01  -9.18e-01  1.938016332e+01   2.180410014e+01   2.4e-01  0.01  
2   2.7e+00  2.4e-01  1.4e-01  5.16e-02   2.492288513e+01   2.574944173e+01   1.2e-01  0.01  
3   5.2e-01  4.7e-02  1.5e-02  3.72e-01   3.668100946e+01   3.697652591e+01   2.4e-02  0.01  
4   1.7e-01  1.5e-02  2.9e-03  9.10e-01   3.822094097e+01   3.832464600e+01   7.7e-03  0.01  
5   2.9e-02  2.7e-03  2.1e-04  9.72e-01   3.895264887e+01   3.897124371e+01   1.3e-03  0.01  
6   5.9e-03  5.4e-04  1.9e-05  9.94e-01   3.908479462e+01   3.908876214e+01   2.7e-04  0.01  
7   9.4e-05  8.5e-06  4.0e-08  9.98e-01   3.912442958e+01   3.912449642e+01   4.3e-06  0.01  
8   1.2e-06  1.1e-07  5.6e-11  1.00e+00   3.912499409e+01   3.912499493e+01   5.4e-08  0.01  
9   1.2e-07  1.1e-08  1.9e-12  1.00e+00   3.912499944e+01   3.912499953e+01   5.5e-09  0.01  
10  5.9e-09  5.4e-10  2.2e-14  1.00e+00   3.912499997e+01   3.912499998e+01   2.7e-10  0.01  
Optimizer terminated. Time: 0.02    


Interior-point solution summary
  Problem status  : PRIMAL_AND_DUAL_FEASIBLE
  Solution status : OPTIMAL
  Primal.  obj: 3.9124999974e+01    nrm: 3e+01    Viol.  con: 3e-08    var: 0e+00    cones: 0e+00  
  Dual.    obj: 3.9124999978e+01    nrm: 2e+00    Viol.  con: 0e+00    var: 3e-09    cones: 0e+00  
Optimizer summary
  Optimizer                 -                        time: 0.02    
    Interior-point          - iterations : 10        time: 0.01    
      Basis identification  -                        time: 0.00    
        Primal              - iterations : 0         time: 0.00    
        Dual                - iterations : 0         time: 0.00    
        Clean primal        - iterations : 0         time: 0.00    
        Clean dual          - iterations : 0         time: 0.00    
    Simplex                 -                        time: 0.00    
      Primal simplex        - iterations : 0         time: 0.00    
      Dual simplex          - iterations : 0         time: 0.00    
    Mixed integer           - relaxations: 0         time: 0.00    

------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): -21.625
 
Done! 
------------------------------------------------------------------------
The computed optimal solution is: 
    1.0000
    0.5000
   -1.0000

The given optimal solution is: 
    1.0000
    0.5000
   -1.0000

</pre>
</div>
</body>
</html>